/**
 * Copyright 2010 Per-Erik Bergman (per-erik.bergman@jayway.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package se.jayway.opengl.tutorial.mesh;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * SimplePlane is a setup class for Mesh that creates a plane mesh.
 * 
 * @author Per-Erik Bergman (per-erik.bergman@jayway.com)
 * 
 */
public class SimplePlane extends Mesh {

	/**
	 * Create a plane with a default with and height of 1 unit.
	 */
	public SimplePlane() {
		float[] floatVertices = { -0.5f, -0.5f, -4, 0.0f, 1.0f, 
									0.5f, -0.5f, -4, 1.0f, 1.0f, 
									-0.5f, 0.5f, -4, 0.0f, 0.0f, 
									0.5f, 0.5f, -4, 1.0f, 0.0f };
		short[] shortIndices = { 0, 1, 2, 1, 3, 2 };
		hasColor = false;
		hasTexCoords = true;

		vertexSize = (3 + (hasColor ? 4 : 0) + (hasTexCoords ? 2 : 0)) * 4;
		int maxVertices = floatVertices.length * 4 / vertexSize;
		tmpBuffer = new int[floatVertices.length];
		int maxIndices = shortIndices.length;
		ByteBuffer buffer = ByteBuffer.allocateDirect(maxVertices * vertexSize);
		buffer.order(ByteOrder.nativeOrder());
		vertices = buffer.asIntBuffer();

		if (maxIndices > 0) {
			buffer = ByteBuffer.allocateDirect(maxIndices * Short.SIZE / 8);
			buffer.order(ByteOrder.nativeOrder());
			indices = buffer.asShortBuffer();
		} else {
			indices = null;
		}

		setVertices(floatVertices);
		setIndices(shortIndices);
		// setTextureCoordinates(textureCoordinates);
	}

	/**
	 * Create a plane.
	 * 
	 * @param width
	 *            the width of the plane.
	 * @param height
	 *            the height of the plane.
	 */
	public SimplePlane(float width, float height) {
		float[] floatVertices = { -10.0f, 0.0f, 10.0f, 0.0f, 1.0f, 
								   10.0f, 0.0f, 10.0f, 1.0f, 1.0f, 
								  -10.0f, 0.0f, -10.0f,  0.0f, 0.0f, 
								   10.0f, 0.0f, -10.0f, 1.0f, 0.0f };
		short[] shortIndices = { 0, 1, 2, 1, 3, 2 };
		hasColor = false;
		hasTexCoords = true;

		vertexSize = (3 + (hasColor ? 4 : 0) + (hasTexCoords ? 2 : 0)) * 4;
		int maxVertices = floatVertices.length * 4 / vertexSize;
		tmpBuffer = new int[floatVertices.length];
		int maxIndices = shortIndices.length;
		ByteBuffer buffer = ByteBuffer.allocateDirect(maxVertices * vertexSize);
		buffer.order(ByteOrder.nativeOrder());
		vertices = buffer.asIntBuffer();

		if (maxIndices > 0) {
			buffer = ByteBuffer.allocateDirect(maxIndices * Short.SIZE / 8);
			buffer.order(ByteOrder.nativeOrder());
			indices = buffer.asShortBuffer();
		} else {
			indices = null;
		}

		setVertices(floatVertices);
		setIndices(shortIndices);
		// setTextureCoordinates(textureCoordinates);

	}
}
